home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 015a / dosimp10.zip / SIMPLY7.HYP < prev    next >
Text File  |  1991-08-12  |  19KB  |  340 lines

  1.                            What Is a |tBatch File|t?
  2.  
  3. Oh, my, this is fun.  Batch files are what got me so interested in learn-
  4. ing how to use DOS.  They're absolutely fascinating!  It is a very simple
  5. sort of Programming, in a way.  But so simple that anyone can do it!
  6.  
  7. Well, a |nbatch file|n is just a plain ASCII text file, that contains nothing
  8. except a batch of DOS commands, each command on a separate line, and each
  9. line ending with a carriage return (the <Enter> key).  The |nbatch file|n can
  10. have any name you want to give it, but it must have the extension .BAT,
  11. and it's a very good idea to not ever give a |nbatch file|n the same name as
  12. any other command on your system.
  13.  
  14. A |nbatch file|n is an executable file, so when you just type its name at the
  15. DOS prompt, and hit <Enter>, the |nbatch file|n will run, just like any other
  16. command.  DOS will read the first line of the |nbatch file|n, and execute the
  17. command, read the next line and execute it, etc., until DOS finds that no
  18. more lines are left to be executed.  That's it!
  19.  
  20.                            <page down> for more
  21.                            Batch File continued
  22. Batch files can contain any commands at all, which includes internal com-
  23. mands that reside inside the COMMAND.COM file, .COM files, .EXE files, or
  24. even other .BAT files (but not CONFIG.SYS commands).  Some internal com-
  25. mands are almost never used for anything else besides batch files.  Those
  26. are CALL, ECHO, FOR, GOTO, IF, PAUSE, REM, and SHIFT.  Replaceable para-
  27. meters is another thing that is only used in batch files.  Each of those
  28. items has its own chapter here, so check them out for more information.
  29.  
  30. Environment variables can be used from within a |nbatch file|n, in a manner
  31. similar to the use of |nreplaceable|n parameters.  If you have an |nenvironment|n
  32. variable named ONE which was set equal to YES via the command |nSET|n ONE=YES
  33. then you could reference that variable from inside a |nbatch file|n by sur-
  34. rounding its name with percent signs, as in %ONE%.  Here's an example:
  35.           |nIF|n %ONE%!==YES! |nGOTO|n OK
  36. Now when DOS is executing the |nbatch file|n that contains that line, it will
  37. see the %ONE% and go look in the |nenvironment|n to find the variable named
  38. ONE, look at what ONE is set equal to, and replace ONE with that string.
  39. So when DOS gets done with that, the above line will look like:
  40.                            <page down> for more
  41.                            Batch File continued
  42.           |nIF|n YES!==YES! |nGOTO|n OK
  43. and so the IF test will come out positive, and the GOTO command will be
  44. executed.  Now this doesn't actually change the |nbatch file|n at all, just
  45. the way DOS interprets the line.  The next time DOS executes the batch
  46. file, it still says |nIF|n %ONE%!==YES! |nGOTO|n OK, but if the environment var-
  47. iable ONE is set equal to NO this time, then when DOS expands that line,
  48. it will say |nIF|n NO!==YES! |nGOTO|n OK this time, so that the |nGOTO|n command will
  49. get ignored because the |nIF|n test fails.
  50.  
  51. The reason for the exclamation point in my examples, is in case you had
  52. forgotten to set the |nenvironment|n variable.  If the ONE variable didn't
  53. exist, then when DOS expands the %ONE% variable, it will find nothing at
  54. all, so the command will be like |nIF|n !==YES! |nGOTO|n OK.  That |nIF|n test will
  55. fail, but at least it won't cause DOS to freak out.  If we left out the !
  56. symbols, and the line said |nIF|n %ONE%==YES |nGOTO|n OK, then when DOS |nexpanded|n
  57. that, it would say |nIF|n ==YES |nGOTO|n OK, if the ONE variable didn't exist,
  58. and since one side of the == signs is empty, DOS won't like that at all,
  59. and you'll get a "Syntax error" message, and the |nbatch file|n will continue
  60.                            <page down> for more
  61.                            Batch File continued
  62. with the next command which might not be what you intended!  So you want
  63. to put some symbol on each side of the == signs to protect from that oc-
  64. currence.  You can use the exclamation point like I did or just about any
  65. symbol you want, as long as it's the same on both sides of the == signs.
  66.  
  67. Here's an example of a way that you can take advantage of environment
  68. variables in a |nbatch file|n.  Suppose there's a certain set of commands
  69. that you would like to execute twice in a row, but not more.  You could
  70. do this:
  71.           |nSET|n AGAIN=
  72.           :START
  73.           (commands you want repeated go here)
  74.           |nIF|n !AGAIN==!NO |nGOTO|n END
  75.           |nSET|n AGAIN=NO
  76.           |nGOTO|n START
  77.           :END
  78.           |nSET|n AGAIN=
  79. Now, when you execute that |nbatch file|n, the first line sets the environ-
  80.                            <page down> for more
  81.                            Batch File continued
  82. ment variable AGAIN equal to nothing, to make sure the variable doesn't
  83. already exist.  The :START line is just a label for the GOTO command that
  84. comes later, so it has no effect the first time through.  Then the main
  85. |nbatch file|n commands are executed, and then comes the IF statement.  Well,
  86. since at this point, AGAIN is not set equal to anything, the |nGOTO|n END
  87. command will be ignored, and the next line, |nSET|n AGAIN=NO will be execu-
  88. ted.  Then the command |nGOTO|n START tells DOS to start over again at the
  89. label that says :START.  So those main commands get executed again, and
  90. then the |nIF|n test is positive this time, because you already executed the
  91. |nSET|n AGAIN=NO line, so this time the |nGOTO|n END command does get executed,
  92. which sends DOS to the line that says :END, and then the AGAIN variable
  93. is once again removed from the environment, and the |nbatch file|n is done.
  94.  
  95. Another way that the use of |nenvironment|n variables from within a batch
  96. file comes in handy, is with the PATH variable.  Suppose that you have a
  97. program that you want to run, for which you need to have that program's
  98. directory on the |nPATH|n, but you don't want to leave that |ndirectory|n on the
  99. |nPATH|n all the time.  Well check out this |nbatch file|n:
  100.                            <page down> for more
  101.                            Batch File continued
  102.           |nSET|n OLD=%PATH%
  103.           |nSET|n PATH=C:\WORD;%OLD%
  104.           WORDPROC
  105.           |nSET|n PATH=%OLD%
  106.           |nSET|n OLD=
  107. Now supposing that your PATH variable started out like C:\DOS;C:\UTIL
  108. then while that |nbatch file|n is being executed, each time you reference
  109. the |nPATH|n or OLD variables, they will be |nexpanded|n to say C:\DOS;C:\UTIL
  110. and here's what the |nbatch file|n will look like to DOS:
  111.           |nSET|n OLD=C:\DOS;C:\UTIL
  112.           |nSET|n PATH=C:\WORD;C:\DOS;C:\UTIL
  113.           WORDPROC
  114.           |nSET|n PATH=C:\DOS;C:\UTIL
  115.           |nSET|n OLD=
  116. So, the first line makes a variable called OLD which is just a duplicate
  117. of what your |nPATH|n variable says at the moment.  The second command sets
  118. a new |nPATH|n variable, which is C:\WORD; followed by what used to be in the
  119. |nPATH|n variable a minute ago.  Then the WORDPROC program gets run, and then
  120.                            <page down> for more
  121.                            Batch File continued
  122. the next line puts the PATH variable back to the way it used to be, by
  123. setting it equal to the OLD variable which you created at the beginning
  124. of the |nbatch file|n.  And the last line removes the OLD variable from the
  125. environment since it's not needed any more, and we don't want to waste
  126. |nenvironment|n space by just leaving it there for no reason.
  127.  
  128. Well speaking of |nPATH|n variables, batch files are an excellent way to keep
  129. your |nPATH|n short.  The only directories that should go on your |nPATH|n, are
  130. those which contain commands that you need to run with some other direc-
  131. tory as the current one.  For commands that you can run from within their
  132. own directory, those commands should not go on the |nPATH|n.  Instead, you
  133. should use a |nbatch file|n (or DOSKEY macro) to make that command's directo-
  134. ry |ncurrent|n, run the program, and change back to the root |ndirectory|n.  Like
  135. this:
  136.           CD C:\WORD
  137.           WORDPROC
  138.           CD \
  139. Now you name that |nbatch file|n WP.BAT, and put it into a |ndirectory|n which
  140.                            <page down> for more
  141.                            Batch File continued
  142. contains nothing but batch files (name it BELFRY, perhaps, or CAVE, since
  143. that's where .BATs go), and put that |nbatch file|n directory on the PATH,
  144. then you can access your WORDPROC program from any |ndirectory|n on your sys-
  145. tem, just by typing WP <Enter>, even if C:\WORD is not on your |nPATH|n.  So,
  146. after you create such a |nbatch file|n for each of your main applications,
  147. now the only directories you need to keep on your |nPATH|n are the one where
  148. you keep your batch files, the one where you keep your DOS external com-
  149. mands, and the one where you keep other third-party utilities that only
  150. have one or two files per program, such that you wouldn't want a separate
  151. |ndirectory|n for each of them.  That last |ndirectory|n you might want to name
  152. UTIL.  So, with only three directories on your |nPATH|n, your whole system
  153. will be a lot more efficient.
  154.  
  155. The problem with batch files is that each one takes up an entire cluster
  156. of disk space, just like any other file, even though batch files are gen-
  157. erally very small.  A 28-byte |nbatch file|n takes up 2048 bytes of space on
  158. a hard disk.  What a waste!  Well you can use replaceable parameters to
  159. combine all of your little batch files into one great big |nbatch file|n in-
  160.                            <page down> for more
  161.                            Batch File continued
  162. stead.  See the section about the GOTO command to find out how.  Now if
  163. you do that, you don't need to keep an entire directory for your batch
  164. files, since you only have one |nbatch file|n.  So just put your big batch
  165. file into the UTIL |ndirectory|n and remove the |nbatch file|n |ndirectory|n from
  166. your disk and from your PATH.
  167.  
  168. If you have DOS version 5.0, you can do just about everything you can do
  169. with batch files, with DOSKEY |nmacros|n instead, except for accessing envir-
  170. onment variables, or using the |nGOTO|n or SHIFT commands.  |nDOSKEY|n |nmacros|n can
  171. not do those things.  And DOSSHELL menu items can do everything except
  172. |nGOTO|n and |nSHIFT|n.  So if you upgrade to DOS version 5 you should convert
  173. all your batch files that don't do those things, to one of those options.
  174. You still need to learn about batch files anyway though, because |nDOSKEY|n
  175. |nmacros|n and |nDOSSHELL|n menu items are put together by the same rules that
  176. batch files follow.
  177.  
  178. And you have to know about batch files in order to keep a nice efficient
  179. AUTOEXEC.BAT file.  That's a very special |nbatch file|n that DOS executes
  180.                            <page down> for more
  181.                            Batch File continued
  182. every time you reboot your computer.  It's the only |nbatch file|n that has
  183. to have a specific name, and that has to be located in the root directory
  184. of the disk you boot from, and that in most cases, you never want to ex-
  185. ecute.  Just let DOS execute it all by itself.  If you want the AUTOEXEC
  186. .BAT to be executed, rebooting is the best way to get that accomplished.
  187. See the AUTOEXEC.BAT chapter for the reasons.
  188.  
  189. You might want to have your |nAUTOEXEC.BAT|n contain just two commands:
  190.           |nECHO|n OFF
  191.           C:\DOS\STARTUP
  192. and then you would have a |nbatch file|n named STARTUP.BAT in your C:\DOS
  193. |ndirectory|n, which the |nAUTOEXEC.BAT|n would run every time you |nreboot|n.  The
  194. reasoning behind this is that a lot of software programs that you install
  195. are going to add commands to your |nAUTOEXEC.BAT|n file.  Well if you have
  196. that really simple two-line |nAUTOEXEC.BAT|n file, then it will be really
  197. easy for you to figure out what changes an installation program made to
  198. that file so you can decide which of those changes you want to keep.  Put
  199. those changes into your STARTUP.BAT file, and remove them from your two-
  200.                            <page down> for more
  201.                            Batch File continued
  202. line |nAUTOEXEC.BAT|n file.  This lets you control your own system, instead
  203. of letting some software programmer control your system for you.
  204.  
  205. Since percent signs are used in batch files to refer to environment var-
  206. iables and replaceable parameters, you have to do something special to
  207. make a |nbatch file|n understand a percent sign in any other context.  Like
  208. if you have a file named HELLO%.TXT, if you mention that filename in a
  209. |nbatch file|n, DOS will flip out over trying to figure out what |nreplaceable|n
  210. parameter or |nenvironment|n variable you're trying to reference.  Because
  211. that's what percent signs are supposed to mean in a |nbatch file|n.  Well all
  212. you need to do, is write that filename with two percent signs whenever
  213. you mention it in a |nbatch file|n.  Just refer to the file as HELLO%%.TXT
  214. whenever you're mentioning it in a |nbatch file|n, and DOS will understand
  215. then, that it's supposed to ignore that particular percent sign as far
  216. as special |nbatch file|n processing is concerned.
  217.  
  218. DOS version 3.3 introduced a special new use for the @ symbol, which is
  219. good for batch files.  If you put @ as the first character of any command
  220.                            <page down> for more
  221.                            Batch File continued
  222. line in a |nbatch file|n, then that particular line will not show up on the
  223. screen while the |nbatch file|n is executing.  For example, you know how the
  224. |nECHO|n OFF command tells DOS not to show the commands on the screen as they
  225. get executed.  But when the |nECHO|n OFF command gets executed, it has not
  226. been executed yet so the |nECHO|n OFF command itself does show on the screen.
  227. Well if you have DOS version 3.3 or later, you can say @ECHO OFF instead
  228. of just |nECHO|n OFF, and then you'll never again see the |nECHO|n OFF command on
  229. your screen.  Isn't that nice?
  230.  
  231. One thing that a lot of people try to accomplish with batch files, that
  232. just won't work, is to feed commands or characters to another program.
  233. Suppose you have a game which, as soon as it's loaded, it asks you wheth-
  234. er you have a color monitor, and you have to tell it Y, and then it asks
  235. you whether you want to use the mouse or the keyboard, and you have to
  236. tell it K, and you're tired of typing those silly keystrokes every single
  237. time you run that game.  Well you might be tempted to write a |nbatch file|n
  238. like this:
  239.  
  240.                            <page down> for more
  241.                            Batch File continued
  242.           GAME
  243.           Y
  244.           K
  245. Well guess what's going to happen when you run that |nbatch file|n?  The game
  246. will run, and it will wait for your answers to those questions just like
  247. always, and then when you're finished playing the game, and you exit to
  248. DOS, you'll see the "|sBad command or filename|s" error message twice.  Why
  249. did that happen?  Well it's because the Y and K lines of the |nbatch file|n
  250. don't get executed until after the GAME command finishes and passes con-
  251. trol back to DOS, so that DOS can read the next command from the batch
  252. file.  And since there are no such commands in DOS, as Y and K, then you
  253. get that error message.
  254.  
  255. One line of a |nbatch file|n does not get executed, in fact does not even get
  256. read by DOS, until the line before it is completely finished.  There's no
  257. way around that, at all.
  258.  
  259. But there are two ways to feed information to a program.  If the program
  260.                            <page down> for more
  261.                            Batch File continued
  262. will accept STanDard INput, you can use the piping form of redirection.
  263. But if the program reads its input straight from the keyboard, rather
  264. than using DOS's normal STanDard INput, then you will need a little util-
  265. ity called a |nKeyboard|n Stuffer instead.  There are quite a few shareware
  266. and public domain |nkeyboard|n stuffers, such as PC Magazine's KEY-FAKE.COM.
  267. These little utilities will feed just about any keystrokes to just about
  268. any program, and they are available for downloading from just about any
  269. BBS in the country.
  270.  
  271. You can use the COPY CON command, or the EDLIN line editor, or if you
  272. have DOS version 5.0, you have the EDIT command.  You can even use the
  273. ECHO command with output |nredirection|n.  All of these things will create
  274. batch files.  |nEDLIN|n and |nEDIT|n can also change batch files that already
  275. exist.  Any text editor, and any word processor that can save files in
  276. plain ASCII format can also do it.
  277.  
  278. There are even cases in which you can use certain DOS commands to create
  279. batch files, from within other batch files, to perform certain functions.
  280.                            <page down> for more
  281.                            Batch File continued
  282. For example, if you execute the PATH command with no parameters, it will
  283. tell you what your |nPATH|n environment variable currently says, like this:
  284.           PATH=C:\DOS;C:\UTIL
  285. Well, that's the exact same format that you use to enter a new |nPATH|n vari-
  286. able!  So if you were to type |nPATH|n > OLDPATH.BAT that'd use redirection
  287. to create a new |nbatch file|n named OLDPATH and you could change your |nPATH|n
  288. to whatever you wanted, and then if you were to later execute OLDPATH
  289. <Enter>, then that PATH=C:\DOS;C:\UTIL command would be executed, to put
  290. your |nPATH|n back to the way it had been before you started!  This sort of
  291. thing can be infinitely useful.  See the end of the chapter about the
  292. TIME command for a really interesting example of that.
  293.  
  294. So check out all the sections that are cross-referenced from this chapter
  295. and have a great time with batch files!
  296.  
  297.  
  298.  
  299.  
  300.  
  301.                          PLEASE IGNORE THIS PAGE!
  302. |tAUTOEXEC.BAT|t|fSIMPLY1|f
  303. |tBBS|t|fSIMPLY1|f
  304. |tDirectory|t|fSIMPLY1|f
  305. |tEnvironment|t|fSIMPLY1|f
  306. |tShareware|t|fSIMPLY1|f
  307. |tbytes|t|fSIMPLY1|f
  308. |tcurrent|t|fSIMPLY1|f
  309. |tdownloading|t|fSIMPLY1|f
  310. |tpublic domain|t|fSIMPLY1|f
  311. |troot|t|fSIMPLY1|f
  312. |tASCII|t|fSIMPLY2|f
  313. |tEDLIN|t|fSIMPLY2|f
  314. |tExecutable|t|fSIMPLY2|f
  315. |tParameters|t|fSIMPLY2|f
  316. |tReplaceable|t|fSIMPLY2|f
  317. |TCALL|T|fSIMPLY3|f
  318. |TCON|T|fSIMPLY3|f
  319. |TCOPY|T|fSIMPLY3|f
  320. |TFOR|T|fSIMPLY3|f
  321. |TSHIFT|T|fSIMPLY3|f
  322. |tBad command or filename|t|fSIMPLY3|f
  323. |tPATH|t|fSIMPLY3|f
  324. |TDOSKEY|T|fSIMPLY4|f
  325. |TECHO|T|fSIMPLY4|f
  326. |TEDIT|T|fSIMPLY4|f
  327. |TGOTO|T|fSIMPLY4|f
  328. |TDOSSHELL|T|fSIMPLY5|f
  329. |TPAUSE|T|fSIMPLY5|f
  330. |TREM|T|fSIMPLY5|f
  331. |tBoot|t|fSIMPLY5|f
  332. |tCOMMAND.COM|t|fSIMPLY5|f
  333. |tReboot|t|fSIMPLY5|f
  334. |tCONFIG.SYS|t|fSIMPLY6|f
  335. |tKeyboard|t|fSIMPLY6|f
  336. |tRedirection|t|fSIMPLY6|f
  337. |tPROMPT|t|fSIMPLY6|f
  338. |TIF|T|fSIMPLY5|f
  339. |TTIME|T|fSIMPLY3|f
  340.